import java.text.*;
import java.util.*;

public class Modell implements Runnable{
	
	ParserControl pc=new ParserControl();
	Map<String, String> Options=new HashMap<String, String>();
	SqlConnection sql=new SqlConnection();
	
	public Map<String, String> getOptions() {
		return Options;
	}

	public Modell()
	{			
	}
	
	//read settings from inits file
	public void initialize()
	{
		sql.SqlInit();
		Options=sql.getRooms();
	}
	
	//set the output file location
	public void setOut() 
	{
		if(Options.containsKey("Output"))
		{
			pc.setOutput(Options.get("Output")+"data.csv");
		}
	}

	//create instace of parsers
	public void setParsers() {
		//PartyGaming rooms
		//TODO extend this list
		if(Options.containsKey("PartyPoker"))
		{
			pc.AddParser(new PartyHandParser(Options.get("PartyPoker"),"PartyPoker"));
		}
		
		//Ipoker rooms
		//TODO extend this list
		if(Options.containsKey("MansionPoker"))
		{
			pc.AddParser(new IpokerHandParser(Options.get("MansionPoker"),"MansionPoker"));
		}
		if(Options.containsKey("VcPoker"))
		{
			pc.AddParser(new IpokerHandParser(Options.get("VcPoker"),"VcPoker"));
		}
		if(Options.containsKey("TitanPoker"))
		{
			pc.AddParser(new IpokerHandParser(Options.get("TitanPoker"),"TitanPoker"));
		}
		if(Options.containsKey("ChiliPoker"))
		{
			pc.AddParser(new IpokerHandParser(Options.get("ChiliPoker"),"ChiliPoker"));
		}
		if(Options.containsKey("WilliamHillPoker"))
		{
			pc.AddParser(new IpokerHandParser(Options.get("WilliamHillPoker"),"WilliamHillPoker"));
		}
		if(Options.containsKey("BetMostPoker"))
		{
			pc.AddParser(new IpokerHandParser(Options.get("BetMostPoker"),"BetMostPoker"));
		}
		
		//OnGame rooms
		//TODO extend this list
		if(Options.containsKey("Bwin"))
		{
			pc.AddParser(new OnGameHandParser(Options.get("Bwin"),"Bwin"));
		}
	}

	public void run()
	{
		ArrayList<GameInfo> eredmeny=null;
		setParsers();
		eredmeny=pc.Perform();
		sql.saveStat(evaulateDatas(eredmeny));
		sql.saveDatas(eredmeny);		
	}

	public Map<String,String> evaulateDatas(ArrayList<GameInfo> al) {
		Map<String, String> eredmeny=new HashMap<String, String>();
		DecimalFormat df=new DecimalFormat("#.##");
		double buyin=0.0;
		double fee=0.0;
		double wins=0.0;
		double first=0.0;
		int second=0;
		for(GameInfo g:al)
		{
			buyin+=g.buyin;
			fee+=g.rake;
			wins+=g.winnings;
			if(g.finishing==1)
				first++;
			else if(g.finishing==2)
				second++;
		}
		eredmeny.put("sumwin", df.format(wins)+"");
		eredmeny.put("sumbuyin", df.format(buyin)+"");
		eredmeny.put("sumfee", df.format(fee)+"");
		eredmeny.put("sumprofit", df.format(wins-fee-buyin)+"");
		eredmeny.put("gamecount", al.size()+"");
		eredmeny.put("roi", df.format((wins-fee-buyin)/(buyin+fee)*100));
		eredmeny.put("hup", df.format((first)/(first+second)*100));
		return eredmeny;
	}
	
	public Map<String,String> getStats()
	{
		return sql.getStat();
	}

	public void save() {
		sql.saveRooms(Options);
	}
	
	public void CreateRoomTable(String s)
	{
		sql.CreateRoomTable(s);
	}
	
	public Set<String> getAvailableRooms(){
		return sql.getAvailableRooms();
	}
	
	public void addParser(HandParser parser)
	{
		pc.AddParser(parser);
	}
}
